var maplet = null;
var marker = null;
$(document).ready(function () {
    initMap();
    initMessage();
    bindEvent();
});

function bindEvent() {
    $('#clear').click(function () {
        maplet.clearOverlays();
        maplet.setMode("bookmark", onBookmark);
    });
}

function initMap() {
    MOUSEWHEEL = false;
    maplet = new Maplet("mmap");
    maplet.centerAndZoom(new MPoint(0, 0), 0);

    document.getElementById('LayerScale').style.display = 'none';
    document.getElementById('ImgCopyright').style.display = 'none';
    $('span:contains("GS(2011)1617")').parent().hide();

    maplet.setMode("bookmark", onBookmark)
    onWindowResize();
}

function initMessage() {
    $("#mmap").mousemove(function (a) {
        var b = a.pageX, c = a.pageY;
        $("#message").css({top: c + 15, left: b + 10})
    }).mouseover(function () {
        $("#message").show();
        var markers = maplet.getMarkers();
        if (null == markers || 0 == markers.length) {
            $("#message").html("在地图上点击鼠标左键定位AP");
        } else {
            $("#message").html("如果您想移动AP位置，请拖动AP放到指定位置");
        }
        if(marker){
            marker.label.setVisible(false);
        }
    }).mouseout(function () {
        $("#message").hide()
        if(marker){
            marker.label.setVisible(true);
        }
    })
}

function onWindowResize() {
    $('#clear').css("left", $('#mmap').position().left);
    $('#clear').css("top", $('#mmap').position().top);
}

function onBookmark(ob) {
    marker = new MMarker(
        ob.point,
        new MIcon("resources/images/wifi-flat.png",32,32),
        null,
        new MLabel("<div class='single'><em></em></div>", {
            xoffset: 0,
            yoffset: 0,
            opacity: 100,
            enableStyle: false
        })
    );

    maplet.addOverlay(marker);
    marker.setEditable(true);
    maplet.setMode("pan");

}

function checkPolyline(polylines) {
    if (polylines == null || polylines.length != 1 || polylines[0].pts == null || polylines[0].pts.split(';').length < 3) {
        $.messager.show({style: {right: '', top: document.body.scrollTop + document.documentElement.scrollTop, bottom: ''}, showType: 'slide',
            title: '错误提示',
            msg: '地图上必须定义一个至少包含三个点的区域（仅一个）！',
            timeout: 5000,
            showType: 'slide'
        });
        return false;
    }

    var pts = polylines[0].pts + ";" + polylines[0].pts.split(";")[0];

    var pts = "POLYGON((" + pts.replaceAll(",", " ").replaceAll(";", ",") + "))";
    $('#fence_polylinePts').val(pts);
    return true;
}